//
//  JHJSpecialViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "JHJSpecialViewController.h"
#import "JHJSpecialCollectionViewCell.h"
#import "CellSpecialModel.h"
#import <AFNetworking.h>
#import "PosterDetailViewController.h"
#import "MJRefresh.h"

@interface JHJSpecialViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, assign)BOOL smallscrollEnable;
@property (nonatomic, assign)NSInteger page;
//@property (nonatomic, copy)NSString *value;

@end

@implementation JHJSpecialViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"sajkdfdjksf");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"==========111111113333333");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"sjakdfds");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor cyanColor];
    self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.array = [NSMutableArray array];
//    NSLog(@"%@",self.value);
    self.page = 1;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:@"deliverValue1" object:nil];
    
    [self createView];
//    self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self addFooter];
}

- (void)addFooter {
    JHJSpecialViewController *specialVC = self;
    [self.collectionView addFooterWithCallback:^{
        specialVC.page++;
        [specialVC createData];
        
    }];
}

- (void)changeValue:(NSNotification *)noti {
    NSLog(@"%@",[noti object]);
    self.value = [noti object];
    if (self.array.count == 0) {
        [self createData];
    }

}

- (void)createData {
    NSString *firstStr = @"http://api.dongting.com/song/singer/";
    NSString *lastStr = @"/albums?page=1&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5&f=f320&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=40d423da80bb81a4af74287284e5ffc7d78bd326&idfa=76D22EE7-1750-4AA8-84F4-9BED5ED94F03&utdid=VutQ7sZhgbgDAPnEjQ1lv%2B0A&alf=201200&bundle_id=com.ttpod.music&latitude=38.88248&longtitude=121.5395";
    NSString *otherStr = @"&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5&f=f320&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=40d423da80bb81a4af74287284e5ffc7d78bd326&idfa=76D22EE7-1750-4AA8-84F4-9BED5ED94F03&utdid=VutQ7sZhgbgDAPnEjQ1lv%2B0A&alf=201200&bundle_id=com.ttpod.music&latitude=38.88248&longtitude=121.5395";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@/albums?page=%ld%@",firstStr, self.value, self.page, otherStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in [responseObject valueForKey:@"data"]) {
            CellSpecialModel *model = [CellSpecialModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        [self.collectionView footerEndRefreshing];
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)createView {
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.itemSize = CGSizeMake((WIDTH - 25) / 2, (WIDTH - 25) / 2 + 40);
    flowLayout.minimumInteritemSpacing = 5;
    flowLayout.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 40) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[JHJSpecialCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([JHJSpecialCollectionViewCell class])];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSmallScrollEnable:) name:@"firstScrollEnable" object:nil];
    self.collectionView.showsVerticalScrollIndicator = NO;
}

- (void)changeSmallScrollEnable:(NSNotification *)noti {
    self.smallscrollEnable = ![[noti object] boolValue];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (!self.smallscrollEnable) {
        scrollView.contentOffset = CGPointZero;
    }else if (scrollView.contentOffset.y < 0) {
        self.smallscrollEnable = NO;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"bingScrollEnable" object:[NSNumber numberWithBool:self.smallscrollEnable]];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JHJSpecialCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([JHJSpecialCollectionViewCell class]) forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    cell.specialModel = self.array[indexPath.row];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CellSpecialModel *model = self.array[indexPath.row];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PUSH" object:model.albumId];
    
    NSLog(@"skjdfjklsdfjslkfjlskfjj");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
